package com.framework.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;

public class LoginPage extends AnyPage {

    public LoginPage(PageManager pages) {
        super(pages);
    }

    @FindBy(name = "username")
    private WebElement usernameField;

    @FindBy(name = "password")
    private WebElement passwordField;

    @FindBy(name = "submit")
    private WebElement submitButton;

    public LoginPage setUsernameField(String text) {
        usernameField.sendKeys(text);
        return this;
    }

    public LoginPage setPasswordField(String text) {
        passwordField.sendKeys(text);
        return this;
    }

    public void clickSubmitButton() {
        submitButton.click();
    }

    public LoginPage ensurePageLoaded() {
        wait.until(presenceOfElementLocated(By.id("loginform")));
        return this;
    }

    public boolean isLoaded() {
        try {
            wait.until(presenceOfElementLocated(By.id("loginform")));
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}